﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UIT.Controls.Containers;
using System.Web;
using System.Linq.Expressions;
using UIT.Controls.Data;
using UIT.Controls.Template;
using UIT.Web.Controls.Data;
using UIT.Web.Controls.Template;
using UIT.Controls;
using System.Xaml;
using System.Web.Compilation;
using System.Reflection;
using System.IO;
using UIT.Web.Renderers;

namespace UIT.Web
{
	public static class ControlExtensions
	{
		public static void RenderControl(this IWebRenderer renderer, IControl control)
		{
			IWebRenderer controlRenderer = (IWebRenderer)renderer.Manager.Control.GetRenderer(control);
			controlRenderer.RunAt = renderer.RunAt;
			controlRenderer.Render(control);
		}

		public static void Render(this IRenderer renderer, ITemplate template)
		{
            renderer.Manager.Template.GetRenderer(template).Render(template);
		}

		public static bool ShouldRenderOnClient(this IDataConsumer self, ITemplate template)
		{
			return (self.DataSource is IClientDataProvider || self.DataSource is EnumerableDataProvider) && template.Metadata != null && ((RunAtMode)template.Metadata) == RunAtMode.Client;
		}

		public static JQueryControlToolkit.JQuery JQuery(this IContainer self, string selector)
		{
			var jquery = new JQueryControlToolkit.JQuery(selector);
			HttpContext.Current.Items.Add(self, jquery);
			return jquery;
		}

		//public static JQueryControlToolkit.JQuery Each(this JQueryControlToolkit.JQuery self, Expression<Action> callback)
		//{
		//    var builder = new JsExpressionBuilder();
		//    builder.Visit(callback);
		//    self.Each(builder.Result.ToString());
		//    return self;
		//}

		//public static JQueryControlToolkit.JQuery Each(this JQueryControlToolkit.JQuery self, Expression<Action<int>> callback)
		//{
		//    var builder = new JsExpressionBuilder();
		//    builder.Visit(callback);
		//    self.Each(builder.Result.ToString());
		//    return self;

		//}
	}
}
